import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Administrator on 2019/6/10
 * 转换器
 */
public class BooleanAndIntConverter extends BaseTypeHandler<Boolean> {

    /**
     * set:java<Boolean> -> DB（int）
     * ps :preparedStatement
     * i :对象参数的操作位置
     * parameter ：java转的值
     * jdbcType :jdbc的操作类型
     */
//    将true和false变成0和1
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Boolean aBoolean, JdbcType jdbcType) throws SQLException {
        if (aBoolean) {
            preparedStatement.setInt(i, 1);
        } else {
            preparedStatement.setInt(i, 0);
        }
    }

    //get：DB ->java 根据列名来拿值
    @Override
    public Boolean getNullableResult(ResultSet resultSet, String s) throws SQLException {
        int anInt = resultSet.getInt(s);
        return anInt == 1 ? true : false;
    }

    //根据下标的形式 （位置）
    @Override
    public Boolean getNullableResult(ResultSet resultSet, int i) throws SQLException {
        int anInt = resultSet.getInt(i);
        return anInt == 1 ? true : false;
    }

    //根据存储过程来拿值
    @Override
    public Boolean getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        int anInt = callableStatement.getInt(i);
        return anInt == 1 ? true : false;
    }


}
